package com.itheima.dao;

import com.itheima.doamin.Student;

import java.util.ArrayList;

public class DaoImpl implements IDao {
    private static ArrayList<Student> list= new ArrayList<>();

    @Override
    public void addStudent(Student s) {
        list.add(s);
    }

    @Override
    public void deleteStudentById(String id) {
        int index = getIndex(id);
        list.remove(index);
    }

    @Override
    public void updateStudent(String id, Student student) {
        int index = getIndex(id);
        list.set(index,student);
    }

    @Override
    public ArrayList<Student> findAllStudents() {
        return list;
    }

    // 手动写的
    public int getIndex(String id){
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getId().equals(id)){
                return i;
            }
        }
        return -1;
    }
}
